package com.nl.controller.web.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsContent;
import com.nl.controller.web.service.SmsContentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("content")
public class SmsContentController {
    @Autowired
    private SmsContentService smsContentService;

    @ApiOperation(value = "查询内容", notes = "分页查询模板内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "地市", required = false, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = false, dataType = "String"),

    })
    @PostMapping("find")
    public Response findContent(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "cityId", required = false) String cityId,
            @RequestParam(value = "mgrId", required = false) String mgrId,
            @RequestParam(value = "mgrName", required = false) String mgrName,
            @RequestParam Map<String, Object> paramMap
    ) {
        if ("".equals(cityId) || cityId == null) {
            throw new SmsException("请选择查询条件，开始查询");
        } else {
            PageInfo<SmsContent> pageInfo = smsContentService.findContent(page, limit, cityId, mgrId,mgrName);
            log.info(" 内容管理参数：{}", paramMap.toString());
            log.debug("内容管理返回数据：{}", pageInfo.getList().toString());
            return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
        }
    }

    @ApiOperation(value = "查询单个短信内容", notes = "查询单个短信内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
    })
    @PostMapping("findContentByMgrId")
    public Response findContentByMgrId(
            @RequestParam(value = "mgrId", required = true) String mgrId ) {
        SmsContent smsContent = null;
        smsContent=smsContentService.findContentByMgrId(mgrId);
        log.info("短信查询参数：{}", mgrId);
        log.info(" 短信查询参数：{}", smsContent);
        if(smsContent==null){
            return ResponseUtil.error(-1,"该项目无内容，请配置");
        }else{
            return ResponseUtil.sucess(smsContent);
        }
    }


}